﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DemoForSnake
{
    /// <summary>
    /// 游戏区域
    /// </summary>
    public class Map
    {
        private Point mapLeft;
        private static int unit = 15;
        //定义地图长，为30个蛇块的长度
        private readonly int MapLength = 30 * unit;
        //定义地图宽，为30个蛇块的宽度
        private readonly int MapWidth = 30 * unit;
        //定义分数
        public int score = 0;
        //定义蛇
        public Snake snake;
        //砖块集合
        private List<Brick> bricks { get; set; }
        public Snake Snake
        {
            get { return snake; }
        }
        Bean food;
        //显示砖块位置
        public Point[] _brickPoints =
        {
            new Point(50,100),
            new Point(100,150),
            new Point(150,200),
            new Point(200,250),
            new Point(250,300),
            new Point(300,350),
            new Point(350,400),
        };
        public Map(Point start)
        {
            //把地图左上角的点的值赋值给全局变量
            mapLeft = start;
            //实例化蛇
            snake = new Snake(start, 5);
            //实例化食物
            food = new Bean();
            food.Origin = new Point(start.X + 30, start.Y + 30);
            //初始化Bricks
            bricks = new List<Brick>();
            foreach (var brickPoint in _brickPoints)
            {
                Brick brick = new Brick(new Rectangle(brickPoint, new Size(15, 15)));
                bricks.Add(brick);
            }
        }
        public int FoodX; //食物X轴
        public int FoodY; //食物Y轴
        //显示新食物
        public void ShowNewFood(Graphics g)
        {
            //消除原先食物
            food.UnShowBean(g);
            //产生随机位置的食物
            food = FoodRandom();
            bool IsCreate = false;
            while (!IsCreate)
            {
                Bean NewFood = new Bean();
                FoodX = NewFood.Origin.X;
                FoodY = NewFood.Origin.Y;
                //判断会不会出现在砖块上
                IsCreate = true;//创建成功
                Rectangle rec = new Rectangle(FoodX, FoodY, 15, 15);
                foreach (var brick in bricks)
                {
                    if (rec.IntersectsWith(brick.Rectangle))
                    {
                        IsCreate = false;
                    }
                    break;
                }
            }
            //显示食物
            food.ShowBean(g);
        }
        //随机产生一个新食物
        private Bean FoodRandom()
        {
            Random d = new Random();
            int x = d.Next(0, MapLength / unit);
            int y = d.Next(0, MapWidth / unit);
            Point origin = new Point(mapLeft.X + x * 15, mapLeft.Y + y * 15);
            Bean food = new Bean();
            food.Origin = origin;
            return food;
        }
        //画地图
        public void ShowMap(Graphics g, Size size)
        {
            //定义空白图
            Image img = new Bitmap(MapLength + 30, MapWidth + 30);
            Graphics _g = Graphics.FromImage(img);

            //创建一支绿笔
            Pen pen = new Pen(Color.Green);
            _g.DrawRectangle(pen, 10, 10, MapLength, MapWidth);
            //显示食物
            food.ShowBean(_g);
            if (CheckBean())
            {
                //吃到了食物
                //显示新食物
                ShowNewFood(_g);
                //蛇变长
                snake.SnakeGrowth();
                //分数增加
                score += 10;
                //显示蛇
                snake.ShowSnake(_g);
            }
            else
            {
                snake.Go(_g);
                snake.ShowSnake(_g);
            }
            //画brick
            foreach (var brick in bricks)
            {
                brick.Draw(_g, brick.Rectangle);
            }
            g.DrawImage(img, new Rectangle(new Point(0, 0), size));
        }
        //判断是否吃到了食物
        public bool CheckBean()
        {
            return food.Origin.Equals(snake.HeadPoint);
        }
        //检查蛇是否撞墙
        public bool CheckSnake()
        {
            return !(snake.getHeadPoint.X > mapLeft.X
            && snake.getHeadPoint.X < (mapLeft.X + MapLength)
            && snake.getHeadPoint.Y > mapLeft.Y
            && snake.getHeadPoint.Y < (mapLeft.Y + MapWidth));
        }

        //处理蛇碰撞砖块
        public void HandleTouch()
        {
            Rectangle rec = new Rectangle(snake.HeadPoint.X, snake.HeadPoint.Y, 15, 15);
            foreach (var brick in bricks)
            {
                if (rec.IntersectsWith(brick.Rectangle))
                {
                    score -= 5;
                    break;
                }
            }
        }
        //重新开始
        public void Reset(Graphics _g)
        {
            snake.UnShowSnake(_g);
            snake.Reset(mapLeft, 5);
        }
    }
}

